
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

function ENT:SetPlayer(ply)
	self:SetPhysicsAttacker(ply)
	self:SetOwner(ply)
end

function ENT:Initialize()

	self:SetModel( "models/props_junk/propane_tank001a.mdl" )
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS ) 
	
	local phys = self:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
	end
end

function ENT:DelayedSound(sound,delay,pitch)
	self.DelaySound = CurTime() + delay
	self.DPitch = pitch
	self.PSound = sound
end

function ENT:DelayedParent(delay)
	self.DelayParent = CurTime() + delay
end

function ENT:SetLauncher(ln)
	self.Launcher = ln
end

function ENT:GetLauncher()
return self.Launcher end

function ENT:FireMaker(owner,pos,min,max,radius)
    for i = 1,math.random(min,max) do
		local rvec = Vector(math.random(radius*-1,radius),math.random(radius*-1,radius),550)
		local fire = ents.Create( "sent_fire" )
		fire:SetPos( pos + rvec )
		fire:SetOwner( owner )
		fire:Spawn()
		fire:Activate()
    end
end

function ENT:ExplosionDamage(owner,pos,damage,radius)

	local tb = ents.FindInSphere(pos,radius) or {}
	
	for k,v in pairs(tb) do
		if v:IsPlayer() then
			v.DeathType = "explosion"
		end
	end
	
	util.BlastDamage( owner, owner, pos, radius, damage )
	util.ScreenShake( pos, math.Rand(2,5),220, math.random(3,6), radius + 50 ) 

	local tracedata = {}
	tracedata.start = pos + Vector(0,0,20)
	tracedata.endpos = pos+Vector(0,0,-250)
	tracedata.filter = self
	local trace = util.TraceLine(tracedata)
	if trace.HitWorld then
		util.Decal("Scorch",trace.HitPos+trace.HitNormal,trace.HitPos-trace.HitNormal)
		local effectdata = EffectData()
		effectdata:SetOrigin(trace.HitPos)
		util.Effect("smoke_crater", effectdata)
	end
	
	for k,v in pairs(tb) do
		v:Extinguish()
		local phys = v:GetPhysicsObject()
		if phys:IsValid() then
			local dir = (v:GetPos() - self:GetPos()):Normalize()
			phys:ApplyForceCenter(dir * ((damage^2) * phys:GetMass()))
		end
		if v:GetClass() == "prop_ragdoll" then
			for i=1,math.random(3,6) do
				local effectdata = EffectData()
				effectdata:SetOrigin(v:GetPos())
				util.Effect("corpse_gib", effectdata)
			end
			v:Remove()
		end
	end
end

function ENT:Think()
	if self.DelaySound then
		if self.DelaySound < CurTime() then
			self:EmitSound(self.PSound,100,self.DPitch)
		end
	end
	if self.DelayParent then
		if self.DelayParent < CurTime() then
			self:SetParent(nil)
			local phys = self:GetPhysicsObject()
			if phys:IsValid() then
				phys:ApplyForceCenter(self:GetOwner():GetVehicle():GetVelocity())
			end
		end
	end
end 

function ENT:OnTakeDamage( dmginfo )
	
	self.Entity:TakePhysicsDamage( dmginfo )

end

function ENT:PhysicsCollide( data, physobj )
	
end